package Test;

import hypertension.*;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

/**
 * This is a sample class to launch a rule.
 */
public class Test {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
            
            //the symptoms of the patient
            Symptoms s1= new Symptoms();
            s1.addToSymptoms("newly HTN");
            s1.addToSymptoms("fall");
            s1.addToSymptoms("IHD");
            System.out.println("Symptoms: "+s1);
            
           //the case Historys of the patient
            Historys h1=new Historys();
             //h1.addToHistorys("IHD");
             //h1.addToHistorys("diabetes mellitus");
            System.out.println("Case Historys: "+h1);
            
            //medicines of the patient
            Medicines m1=new Medicines();
            m1.change=false;
            System.out.println("medicine change?: " + m1.change);
 
            
            //the indicators of the patient
           // Object[] I= {new Indicator("BP", 129),new Indicator("BP", 130)};
            Indicator I1=new Indicator("systolic BP",150);
            Indicator I2=new Indicator("systolic BP",160);
            Indicator I3=new Indicator("diastolic BP",180);
            Indicator I4=new Indicator("new systolic BP",180);
            
            ksession.insert(s1);
            ksession.insert(h1);
            ksession.insert(m1);
            //ksession.insert(m2);
            ksession.insert(I1);
            ksession.insert(I2);
            ksession.insert(I3);
            ksession.insert(I4);
            ksession.fireAllRules();
            logger.close();
         
            
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("HTN.drl"), ResourceType.DRL);
        //kbuilder.add(ResourceFactory.newClassPathResource("GoalBP.drl"), ResourceType.DRL);
        //kbuilder.add(ResourceFactory.newClassPathResource("newlyHTN.drl"), ResourceType.DRL);
        //kbuilder.add(ResourceFactory.newClassPathResource("withoutHTN.drl"), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }

}
